package com.luping.xiao.xiao.controller;

import com.luping.xiao.xiao.service.SysUserService;
import com.luping.xiao.xiao.utils.Thread.TestThreadPool;
import com.luping.xiao.xiao.utils.Thread.ThreadFactoryRun;
import com.luping.xiao.xiao.utils.Thread.TimerRun;
import com.luping.xiao.xiao.utils.controller.BaseController;
import com.luping.xiao.xiao.utils.demoin.AjaxResult;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

@Controller
public class IndexController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(100,
            new BasicThreadFactory.Builder().namingPattern("task-pool-%d").daemon(true).build());


    @Autowired
    private SysUserService userService;
    @Autowired
    private ExecutorService getSynchronousQueueExecutorService;


    @Autowired
    private ExecutorService getLinkedBlockingQueueExecutorService;


    @Autowired
    private ExecutorService getArrayBlockingQueueExecutorService;


    @RequestMapping("index")
    @ResponseBody
    public AjaxResult Index(String name, Integer age) {
        log.info("controller=====");
//        SysUser u = new SysUser();
//        u.setUserName("T" + (int) Math.random() * 1000);
//        u.setAge((int) Math.random() * 100);
//        userService.save(u);
        for (int i = 0; i < 10000; i++) {
            TestThreadPool.productMethod(name, age);
        }
        TimerRun.init();
        return success(userService.list().toString());
    }

    @RequestMapping("t1")
    @ResponseBody
    public AjaxResult te1(Integer n) {
        for (int i = 0; i < n; i++) {
            getSynchronousQueueExecutorService.execute(new ThreadFactoryRun(i));
        }
        return success();
    }

    @RequestMapping("t2")
    @ResponseBody
    public AjaxResult te2(Integer n) {
        for (int i = 0; i < n; i++) {
            getLinkedBlockingQueueExecutorService.execute(new Runnable() {
                @Override
                public void run() {
                    addNum(n);
                    log.info(Thread.currentThread().getName());
                }
            });
        }
        return success();
    }

    @RequestMapping("t3")
    @ResponseBody
    public AjaxResult te3(Integer n) {
        for (int i = 0; i < n; i++) {
            getArrayBlockingQueueExecutorService.execute(new Runnable() {
                @Override
                public void run() {
                    addNum(n);
                    log.info(Thread.currentThread().getName());
                }
            });
        }
        return success();
    }

    public static int addNum(int n) {
        int i = 1;
        int sum = 0;
        while (i <= n*10) {
            sum = sum + i;
            i++;
        }
        return sum;
    }


}
